package se392.ateam2006.meetingmgmt.meeting;

import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.persistence.CascadeType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.ejb.EntityContext;
import se392.ateam2006.meetingmgmt.MeetingMgmtBean;

import se392.ateam2006.resourcemgmt.room.RoomEntity;
import se392.ateam2006.user.UserEntity;

/**
 * Entity class to define the attributes and operations of a Meeting (start, end times, etc)
 * This information can then be persisted into a database
 * @author Ateam (Matthew Bennett, Claire Melton, Shingai Manyiwa, John Adderley)
 * @version 25/03/07
 */
@Entity
public class MeetingEntity implements Serializable {
    
    @Id
    private String id;
    private String title;
    private String description;
    @OneToOne
    private UserEntity initiator;
    @Temporal(TemporalType.DATE)
    private Calendar startDateRange;
    @Temporal(TemporalType.DATE)
    private Calendar endDateRange;
    @Temporal(TemporalType.DATE)
    private Calendar startDate;
    @Temporal(TemporalType.DATE)
    private Calendar endDate;
    @OneToOne
    private Duration duration;
    private int requestedRoomSize = -1;
    private String equipment = null;
    @OneToOne
    private RoomEntity room;
    private int cutOffPeriod = MeetingMgmtBean.defaultCutOffPeriod;
    private boolean booked = false;
           
    /**
     * Default constructor required as this is an entity class
     */
    public MeetingEntity() { }
    
    /**
     * Creates a new instance of MeetingEntity
     * @param initiator - the UserEntity who initiated the meeting
     * @param id - String, an id for the meeting
     * @param title - String, the title of the meeting
     * @param description - String, a description of the meeting
     * @param startDateRange - Calendar, the start of the range that the meeting may exist in
     * @param endDateRange - Calendar, the end of the range that the meeting may exist in
     * @param duration - Duration, the duration of the meeting
     * @param equipment - String, the equipment needed for the meeting
     */
    public MeetingEntity(UserEntity initiator, String id, String title, String description,
                      Calendar startDateRange, Calendar endDateRange,
                      Duration duration, String equipment) {
        
        this(initiator,id,title,description,startDateRange,endDateRange,null,null,duration,-1,equipment,null,1,false);
    } 
    
    /**
     * Creates a new instance of MeetingEntity
     * @param initiator - the UserEntity who initiated the meeting
     * @param id - String, an id for the meeting
     * @param title - String, the title of the meeting
     * @param description - String, a description of the meeting
     * @param startDateRange - Calendar, the start of the range that the meeting may exist in
     * @param endDateRange - Calendar, the end of the range that the meeting may exist in
     * @param duration - Duration, the duration of the meeting
     * @param requestedRoomSize - int, the number of people that we need to accomodate
     * @param equipment - String, the equipment needed for the meeting
     * @param room - RoomEntity, the room that we want the meeting to be in 
     */
    public MeetingEntity(UserEntity initiator, String id, String title, String description,
                      Calendar startDateRange, Calendar endDateRange,
                      Duration duration, int requestedRoomSize, String equipment, RoomEntity room) {
        
        this(initiator,id,title,description,startDateRange,endDateRange,null,null,duration,requestedRoomSize,equipment,room,1,false);
    }        
    
    /**
     * Creates a new instance of MeetingEntity
     * @param initiator - the UserEntity who initiated the meeting
     * @param id - String, an id for the meeting
     * @param title - String, the title of the meeting
     * @param description - String, a description of the meeting
     * @param startDateRange - Calendar, the start of the range that the meeting may exist in
     * @param endDateRange - Calendar, the end of the range that the meeting may exist in
     * @param startDate - Calendar, the start date/time of the meeting
     * @param endDate - Calendar, the end date/time of the meeting
     * @param duration - Duration, the duration of the meeting
     * @param requestedRoomSize - int, the number of people that we need to accomodate
     * @param equipment - String, the equipment needed for the meeting
     * @param room - RoomEntity, the room that we want the meeting to be in
     * @param cutOffPeriod - int, the number of days before a meeting when you are unable to modify
     *      any related exclusion/preference sets
     * @param booked - boolean, is this meeting booked? 
     */
    public MeetingEntity(UserEntity initiator, String id, String title, String description,
                      Calendar startDateRange, Calendar endDateRange,
                      Calendar startDate, Calendar endDate,
                      Duration duration, int requestedRoomSize, String equipment, 
                      RoomEntity room, int cutOffPeriod, boolean booked) {
        
        this.initiator = initiator;
        this.id = id;
        this.title = title;
        this.description = description;
        this.startDateRange = startDateRange;
        this.endDateRange = endDateRange;
        this.startDate = startDate;
        this.endDate = endDate;
        this.duration = duration;       
        this.requestedRoomSize = requestedRoomSize;      
        this.equipment = equipment;  
        this.cutOffPeriod = cutOffPeriod;
        this.booked = booked;
        this.room = room;
    }         
    
    /**
     * Get the ID of the meeting entity
     * @return String - the ID of the meeting
     */
    public String getId() {
        return id;
    }

    /**
     * Set the ID of the meeting
     * @param id - the new ID of the meeting
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Get the title of the meeting entity
     * @return String - the title of the meeting
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the title of the meeting
     * @param title - the new title of the meeting
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Get the description of the meeting entity
     * @return String - the description of the meeting
     */
    public String getDescription() {
        return description;
    }

    /**
     * Set the description of the meeting
     * @param description - the new description of the meeting
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Get the start of the date range for the meeting entity
     * @return Calendar - the start date/time of the date range for the meeting
     */
    public Calendar getStartDateRange() {
        return startDateRange;
    }

    /**
     * Set the start of the date range for the meeting
     * @param startDateRange - Calendar containing the new start date/time of the meeting
     */
    public void setStartDateRange(Calendar startDateRange) {
        this.startDateRange = startDateRange;
    }

    /**
     * Get the end of the date range for the meeting entity
     * @return Calendar - the end date/time of the date range for the meeting
     */
    public Calendar getEndDateRange() {
        return endDateRange;
    }

    /**
     * Set the end of the date range for the meeting
     * @param endDateRange - Calendar containing the new end date/time of the meeting
     */
    public void setEndDateRange(Calendar endDateRange) {
        this.endDateRange = endDateRange;
    }
    
    /**
     * Override the equals method...
     * @param other - the object to compare to
     * @return boolean - are the objects equal?
     */
    public boolean equals(Object other){
        
        if(!(other instanceof MeetingEntity)) {
            
            return false;
        }
        else {

            MeetingEntity meet = (MeetingEntity)other;
            
            if(this.id.equals(meet.getId()))
            {    
                
               return true;
            }
            else {
                
               return false;
            }
        }
    }

    /**
     * Get the requested room size
     * @return int - the requested room size
     */
    public int getRequestedRoomSize() {
        return requestedRoomSize;
    }

    /**
     * Set the requested room size
     * @param roomSize - the new room size required
     */
    public void setRequestedRoomSize(int roomSize) {
        this.requestedRoomSize = roomSize;
    }

    /**
     * Get any requested equipment
     * @return String - a string containing the requested equipment
     */
    public String getEquipment() {
        return equipment;
    }
    
    /**
     * Set the equipment needed for this meeting
     * @param equipment - string containing the required equipment
     */
    public void setEquipment(String equipment) {
        this.equipment = equipment;
    }

    /**
     * Get the room that this meeting will take place in
     * @return RoomEntity - the meeting's room
     */
    public RoomEntity getRoom() {
        return room;
    }

    /**
     * Set the room where this meeting will take place
     * @param room - the new RoomEntity where the meeting will happen
     */
    public void setRoom(RoomEntity room) {
        this.room = room;
    }

    /**
     * Get the duration of this meeting
     * @return Duration - the duration of the meeting
     */
    public Duration getDuration(){
        return this.duration;
    }
    
    /**
     * Set the duration of the meeting
     * @param duration - the new duration of the meeting
     */
    public void setDuration(Duration duration){
        this.duration = duration;
    }

    /**
     * Get the cut off period for this meeting (number of days)
     * @return int - number of days in cur off period
     */
    public int getCutOffPeriod() {
        return cutOffPeriod;
    }
    
    /**
     * Set the cut off period for the meeting (number of days)
     * @param cutOffPeriod - the new cut off period
     */
    public void setCutOffPeriod(int cutOffPeriod) {
        this.cutOffPeriod = cutOffPeriod;
    }  

    /**
     * Get whether the meeting is booked
     * @return boolean - is the meeting booked?
     */
    public boolean isBooked() {
        return booked;
    }
    
    /**
     * Set whether the meeting is booked
     * @param booked - the new value of the booked flag
     */
    public void setBooked(boolean booked) {
        this.booked = booked;
    }

    /**
     * Get the start date/time of the meeting
     * @return Calendar - the start date/time of the meeting
     */
    public Calendar getStartDate() {
        return startDate;
    }

    /**
     * Set the start date/time of the meeting
     * @param startDate - Calendar contining the new date/time for the start of the meeting
     */
    public void setStartDate(Calendar startDate) {
        this.startDate = startDate;
    }

    /**
     * Get the end date of the meeting
     * @return Calendar - the end date/time of the meeting
     */
    public Calendar getEndDate() {
        return endDate;
    }

    /**
     * Set the end date/time of the meeting
     * @param endDate - Calendar contining the new date/time for the end of the meeting
     */
    public void setEndDate(Calendar endDate) {
        this.endDate = endDate;
    }
    
    /**
     * Get the initiator of the meeting
     * @return UserEntity - the initiator of the meeting
     */
    public UserEntity getInitiator() {
        return initiator;
    }

    /**
     * Set the initiator of the meeting
     * @param initiator - the UserEntity who initiated the meeting
     */
    public void setInitiator(UserEntity initiator) {
        this.initiator = initiator;
    }
}
